package org.hawksee.core.tools;

import java.util.AbstractList;
import java.util.ArrayList;

import org.hawksee.core.data.BaseMonochromeImage;

/*
 * This class manages a series of images, allowing operations on them.
 */
public class Manager
{
  private ArrayList<BaseMonochromeImage> images;
  private BaseMonochromeImage selectedImage = null;
  private BaseMonochromeImage targetedImage = null;
  private Registrator registrator;
  
  public Manager()
  {
    this.registrator = new Registrator();
    this.images = new ArrayList<BaseMonochromeImage>();
  }
  /*
   * Adds an image
   */
  public boolean addImage(BaseMonochromeImage image)
  {
    if (this.images.contains(image)) {
      return false;
    }
    this.images.add(image);
    return true;
  }
  /*
   * Returns the images
   */
  public AbstractList<BaseMonochromeImage> getImages()
  {
    return this.images;
  }
  /*
   * Gets the registrator
   */
  public Registrator getRegistrator()
  {
    return this.registrator;
  }
  /*
   * Returns whether or not the image is selected
   */
  public boolean isSelected(BaseMonochromeImage image)
  {
    return (this.selectedImage == image);
  }
  /*
   * Returns whether or not the image is selected
   */
  public boolean isTargeted(BaseMonochromeImage image)
  {
    return (this.targetedImage == image);
  }
  /*
   * Moves selected image
   */
  public void moveSelected(int x, int y, boolean absolute)
  {
    if (this.selectedImage == null) {
      return;
    }
    if (absolute) {
      // absolute move
      this.selectedImage.moveTo(x, y);
    } else {
      // relative move
      this.selectedImage.moveBy(x, y);
    }
  }
  /*
   * Resets the state
   */
  public void reset()
  {
    this.selectedImage = null;
    this.targetedImage = null;
    this.images.clear();
  }
  /*
   * Registers the selected image to the target
   */
  public boolean register()
  {
    if ((this.targetedImage == null) || (this.selectedImage == null)) {
      return false;
    }
    return this.registrator.moveToOptimal(this.targetedImage, this.selectedImage);
  }
  /*
   * Selects the first image containing the point
   */
  public boolean selectImage(int x, int y)
  {
    this.selectedImage = null;
    for (BaseMonochromeImage image : this.images) {
      if (image == this.targetedImage) {
        continue;
      }
      if (image.getRegion().contains(x, y)) {
        this.selectedImage = image;
      }
    }    
    // put selected image at bottom to be drawn last
    if (this.selectedImage != null) {
      this.images.remove(this.selectedImage);
      this.images.add(this.selectedImage);
    }
    return (this.selectedImage != null);
  }
  /*
   * Selects an image
   */
  public boolean selectImage(BaseMonochromeImage image)
  {
    this.selectedImage = image;
    return (this.selectedImage != null);
  }
  /*
   * Targets an image from the current selection
   */
  public boolean targetImage()
  {
    return this.targetImage(this.selectedImage);
  }
  /*
   * Targets an image
   */
  public boolean targetImage(BaseMonochromeImage image)
  {
    if (this.selectedImage == image) {
      this.selectedImage = null;
    }
    this.targetedImage = image;
    return (this.targetedImage != null);
  }
}
